package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SpringView;
import com.wude.bean.JifenDetailBean;
import com.wude.bean.TrafficDetailBean;
import com.wude.guild.MineManagerActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.JifenDetailAdapter;
import com.wude.guild.servercore.mints.MintsBaseActivity;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.guild.views.MyListView;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 唐小妹 on 2017/12/11 0011.
 */

public class JifenDetailFragment extends TitleBaseFragment implements View.OnClickListener {
    private SpringView springview;
    private MyListView lv_detail;
    private RelativeLayout rl_mineJifen;
    private TextView tv_point;

    private JifenDetailAdapter adapter;
    private String tag = "default";

    private List<JifenDetailBean> dataList = new ArrayList<>();
    private int limit = 1;
    private boolean isMore = false;
    private MintsBaseActivity activity;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.jifen_detail_fragment, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MintsBaseActivity) activity;
    }

    @Override
    public void onEnter(Object data) {
        super.onEnter(data);
        if (data != null)
            tag = (String) data;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHeaderTitle("积分明细");
        initView(view);

        ImageView rightImageView = getTitleHeaderBar().getRightImageView();
        rightImageView.setImageResource(R.mipmap.jifen_rule);
        rightImageView.setOnClickListener(this);

        initAndReflsh(false);
    }

    private void initView(View view) {
        lv_detail = findView(view, R.id.lv_detail);
        rl_mineJifen = findView(view, R.id.rl_mineJifen);
        springview = findView(view, R.id.springview);
        tv_point = findView(view, R.id.tv_point);

        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
//        springview.setHeader(new SimpleHeader(getActivity()));
        springview.setFooter(new SimpleFooter(getActivity()));

        switch (tag) {
            case "default":
                rl_mineJifen.setVisibility(View.VISIBLE);
                break;
            case "jifenRecord":
                rl_mineJifen.setVisibility(View.GONE);
                break;
        }

        adapter = new JifenDetailAdapter(getActivity());
        lv_detail.setAdapter(adapter);
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh(false);
            isMore = false;
        }

        @Override
        public void onLoadmore() {
            initAndReflsh(true);
            isMore = true;
        }
    };

    private void initAndReflsh(boolean isMore) {
        if (!isMore) {
            dataList.clear();
            limit = 1;
        } else
            limit = ++limit;
        //数据请求
        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        map.put("p", limit + "");

        switch (tag) {
            case "default":
                HttpCom.POST(handler, HttpCom.getGetPointDetail(), map, false);
                break;
            case "jifenRecord":
                HttpCom.POST(handler, HttpCom.getGetPointShopRecordDetails(), map, false);
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    DNSData(msg.obj.toString());
                    break;
                case 2:
                    ToastUtil.showToast("网络错误");
                    springview.setVisibility(View.GONE);
                    break;
            }
        }
    };

    private void DNSData(String str) {
        try {
            JSONObject jsonObject = new JSONObject(str);
            int status = jsonObject.getInt("status");
            Log.d("积分明细", str);
            if (status == 1) {
                JSONObject data = jsonObject.getJSONObject("data");
                tv_point.setText("现有积分：" + data.getString("point"));
                JSONArray detail = data.getJSONArray("data");
                if (detail != null) {
                    for (int i = 0; i < detail.length(); i++) {
                        JSONObject detailObj = detail.getJSONObject(i);
                        JifenDetailBean bean = new JifenDetailBean();
                        bean.addtime = detailObj.getString("addtime");
                        bean.desc = detailObj.getString("desc");
                        bean.point = detailObj.getString("point");
                        dataList.add(bean);
                    }
                    adapter.setData(dataList);
                } else {
                    ToastUtil.showToast(isMore ? "已经到底了" : "没有数据哦");
                }
            } else {
                ToastUtil.showToast(jsonObject.getString("return_code"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        activity.pushFragmentToBackStack(JifenRuleFragment.class, null);
    }
}
